package kr.ac.hufs;

import java.awt.TextArea;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

public class utilFunc {
	
	public void debug(StringBuffer command,TextArea textArea) {
		if(var.DEBUG_MODE == var.OFF) {
			return;
		}else if(var.DEBUG_MODE == var.ON) 
		{
			textArea.append("Request Message : "+command);	
		}
	}
	
	public Socket listenFTPServConn() throws IOException {
		
		ServerSocket serverSocket =null;
		Socket socket =null;
		
		serverSocket = new ServerSocket(var.ACTIVE_PORT_NUM);
		socket = serverSocket.accept();
		
		return socket;

		//Close(connfd);

	}
	
	public String getMyAddr(Socket socket) throws UnknownHostException {
		String ip;
		InetAddress Inet = socket.getLocalAddress();
		ip = Inet.getHostAddress();
		return ip;
	}
	
	public void makeActiveMsg(String ip,StringBuffer activeMsg) {

		int i = 0;
		StringBuffer temp = new StringBuffer();
		temp.append(ip);		
		for (i = 0; i < temp.length(); i++) {
			if (temp.charAt(i) == 0)
				break;
			if (temp.charAt(i) == '.')
				temp.replace(i, i+1,",");
		}
		activeMsg.append("PORT "+temp+","+var.ACTIVE_PORT_NUM /256+","+var.ACTIVE_PORT_NUM %256+"\r\n");
	}	
	public int execLCDFunc(StringBuffer command) {
				
		int n;				
		int i =0;
		String tmp;
		tmp = command.toString();
		String tmp2;
		String[] word = new String[2];
		
		StringTokenizer stk = new StringTokenizer(tmp);
		int wordN = stk.countTokens();		
		while(stk.hasMoreTokens()){
		word[i] =stk.nextToken();
		i++;
		}
		StringBuffer temp = new StringBuffer();				
		if(wordN == 1) {
			temp.append(System.getProperty("user.dir"));
			System.out.println(temp);
		} else if(wordN == 2) {
			
			temp.append(System.getProperty("user.dir") + "\\" +word[1]);
			System.out.println(temp);		
		}
		
		return 1;
	}
	

public int makeClientCommand(StringBuffer buf, StringBuffer command, StringBuffer PATHNAME) {
		
		int i =0;
		String tmp;
		tmp = buf.toString();
		String[] word = new String[10];
		word[0]= "";
		word[1]= "";

		StringTokenizer stk = new StringTokenizer(tmp);
		int wordN = stk.countTokens();		
		while(stk.hasMoreTokens()){
			word[i] =stk.nextToken();
			System.out.println(word[i]);
			i++;
			}
		if ((word[0].equals("open")== true) || (word[0].equals("user")==true)) {
			return var.OPEN;
		}		

		else if ((word[0].equals("ls")== true) || (word[0].equals("dir")== true)
				|| (word[0].equals("nlist")== true)) {
			if (wordN == 1) {
				command.append("LIST\r\n");
			} else if (wordN == 2) {
				command.append("LIST ");
				command.append(word[1]+"\r\n");
			}
			return var.LS;
		} else if (word[0].equals("pwd")== true) {
			command.append("PWD\r\n");			
			return var.PWD;
		} else if (word[0].equals("lcd")== true) {
			return var.LCD;
		}
		else if ((word[0].equals("cd")== true)) {
			command.append("CWD ");
			command.append(word[1]+"\r\n");
			return var.CD;
		}

		else if ((word[0].equals("type")== true)) { // only typing type
			if (wordN == 1) {
				return var.TYPE;
			} else if (wordN == 2) {
				if (word[1].equals("binary")==true) {
					command.append("TYPE B\r\n");
					var.BINARY_MODE = var.ON;
				} else if (word[1].equals("ascii")==true) {
					command.append("TYPE A\r\n");					
					var.BINARY_MODE = var.OFF;
				}
				return var.TYPE_CHANGE;
			}
		} else if (word[0].equals("ascii")==true) {
			command.append("TYPE A\r\n");
			var.BINARY_MODE = var.OFF;
			return var.TYPE_CHANGE;
		}
		else if (word[0].equals("binary")==true) {
			command.append("TYPE B\r\n");
			var.BINARY_MODE = var.ON;
			return var.TYPE_CHANGE;
		} else if (word[0].equals("get")==true) {
			command.append("RETR ");
			command.append(word[1]+"\r\n");
			PATHNAME.append(word[1]);
			return var.GET;
		} else if (word[0].equals("put")==true) {
			command.append("STOR ");
			command.append(word[1]+"\r\n");
			PATHNAME.append(word[1]);
			return var.PUT;
		} else if (word[0].equals("close")==true) {
			command.append("QUIT\r\n");
     		return var.CLOSE;
		} else if ((word[0].equals("quit")==true)|| (word[0].equals("bye")==true)) {
			command.append("QUIT\r\n");
			return var.QUIT;
		} else if (word[0].equals("system")==true) {
			command.append("SYST\r\n");
			return var.SYSTEM;
		}	else if (word[0].equals("passive")==true) {
			return var.PASSIVE;
		} else if (word[0].equals("debug")==true) {
			return var.DEBUG;
		}
		else {
			return var.ELSE;
		}
		return 0;	
	}
}

	
